import os
import zipfile
from datetime import datetime

def BackupWebFolder():
    #bakcup web folder
    now = datetime.now()
    now_str = now.strftime('%Y%m%d_%H%M')
    cmd = '7z a e:\\SelfBackup\\www_%s.zip \"D:\\wamp\\www\\*\" -r' % (now_str)
    os.system(cmd)

def BackupMysql(host, user, password, dbname):
    #bakcup mysql
    sqlfile = 'd:\\'+dbname+'.sql'
    cmd = 'mysqldump -h%s -u%s -p%s %s > %s' % (host, user, password, dbname, sqlfile)
    print(cmd)
    if os.system(cmd)==0:
        now = datetime.now()
        now_str = now.strftime('%Y%m%d_%H%M')
        zip_filename = 'E:\\SelfBackup\\'+dbname+'_'+now_str+'.zip'
        zipf = zipfile.ZipFile(zip_filename, 'w' ,zipfile.ZIP_DEFLATED)
        zipf.write(sqlfile)
        zipf.close()
        print(zip_filename)
        
if __name__ == '__main__':
    path_str = os.environ['PATH']+r'C:\Program Files\7-Zip;'+r'D:\wamp\bin\mysql\mysql5.5.24\bin;'
    os.environ['PATH']= path_str
    
    BackupWebFolder()
    BackupMysql('localhost','jomdurmin','hong1122','jomdurmin')
    BackupMysql('localhost','hmailserver','hong1122','hmailserver')
    BackupMysql('localhost','wordpress','hong1122','wordpress')
    BackupMysql('localhost','roundcube','hong1122','roundcube')
    #BackupMysql('localhost','root','hong1122','jom16_durmin_fashion')
    print('end')